﻿Public Class ctrGoiMon

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        frmMain.TabControl1.TabPages.Remove(frmMain.TabControl1.SelectedTab)
    End Sub

    Private Sub rdMonAn_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdMonAn.CheckedChanged
        Dim bus As GeneralBUS = New GeneralBUS()
        gvThucDon.DataSource = bus.GetTable("select * from THUCDON where ThucUong=False")
    End Sub

    Private Sub rdThucUong_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdThucUong.CheckedChanged
        Dim bus As GeneralBUS = New GeneralBUS()
        gvThucDon.DataSource = bus.GetTable("select * from THUCDON where ThucUong=True")
    End Sub

    Private Sub rdTatCa_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdTatCa.CheckedChanged
        gvThucDon_Load()
    End Sub

    Private Sub gvThucDon_Load()
        Dim bus As ThucDonBUS = New ThucDonBUS()
        gvThucDon.DataSource = bus.GetTable()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim tmp As ChiTietHoaDonDTO = New ChiTietHoaDonDTO()
        tmp.MaHD = gvChiTietHD.CurrentRow.Cells(0).Value.ToString()
        tmp.MaMon = gvChiTietHD.CurrentRow.Cells(1).Value.ToString()

        Dim bus As ChiTietHoaDonBUS = New ChiTietHoaDonBUS()
        bus.Delete(tmp)
        gvChiTietHD_Load()
    End Sub

    Private Sub gvBan_Load()
        Dim bus As GeneralBUS = New GeneralBUS()
        gvBan.DataSource = bus.GetTable("select * from BAN where TinhTrang='Đầy'")
    End Sub

    Private Sub gvChiTietHD_Load()
        Dim bus As GeneralBUS = New GeneralBUS()
        gvChiTietHD.DataSource = bus.GetTable("select CT.MaHD, CT.MaMon, TenMon, SoLuong, ThanhTien" _
                                              & " from CHITIETHOADON CT, HOADON HD, THUCDON TD" _
                                              & " where TD.MaMon=CT.MaMon and CT.MaHD=HD.MaHD and MaBan=" + gvBan.CurrentRow.Cells(0).Value.ToString())
    End Sub

    Private Sub ctrGoiMon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gvBan_Load()
        gvThucDon_Load()
        gvChiTietHD_Load()
    End Sub

    Private Sub gvBan_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvBan.SelectionChanged
        gvChiTietHD_Load()
    End Sub

    Private Sub btnSoLuong_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSoLuong.Click
        Dim tmp As ChiTietHoaDonDTO = New ChiTietHoaDonDTO()
        tmp.MaHD = gvChiTietHD.CurrentRow.Cells(0).Value.ToString()
        tmp.MaMon = gvChiTietHD.CurrentRow.Cells(1).Value.ToString()
        tmp.SoLuong = Integer.Parse(txtSoLuong.Text)

        Dim bus2 As GeneralBUS = New GeneralBUS()
        Dim dongia As Integer = Integer.Parse(bus2.GetValue("select DonGia from THUCDON where MaMon=" + gvChiTietHD.CurrentRow.Cells(1).Value.ToString()))
        tmp.ThanhTien = tmp.SoLuong * dongia

        Dim bus As ChiTietHoaDonBUS = New ChiTietHoaDonBUS()
        bus.Update(tmp)
        gvChiTietHD_Load()
    End Sub

    Private Sub gvChiTietHD_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvChiTietHD.SelectionChanged
        If gvChiTietHD.CurrentRow IsNot Nothing Then
            lbTenMon.Text = gvChiTietHD.CurrentRow.Cells(2).Value.ToString()
            txtSoLuong.Text = gvChiTietHD.CurrentRow.Cells(3).Value.ToString()
        End If
    End Sub

    Private Sub gvThucDon_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvThucDon.DoubleClick
        Dim tmp As ChiTietHoaDonDTO = New ChiTietHoaDonDTO()
        tmp.MaHD = gvChiTietHD.CurrentRow.Cells(0).Value.ToString()
        tmp.MaMon = gvThucDon.CurrentRow.Cells(0).Value.ToString()
        tmp.SoLuong = 1
        tmp.ThanhTien = gvThucDon.CurrentRow.Cells(4).Value.ToString()

        Dim bus As ChiTietHoaDonBUS = New ChiTietHoaDonBUS()
        bus.Insert(tmp)
        gvChiTietHD_Load()
    End Sub
End Class
